<?php

namespace Home\Controller;

use Think\Controller;
use Org\Util\Date;

class CartController extends AuthController {
	public function index() {
		$store = session ( 'store' );
		$where ['uid'] = $store ['id'];
		/*
		 * 获取地址列表
		 */
		$list = M ( "Address" )->where ( $where )->select ();
		$count = count ( $list );
		for($i = 0; $i < $count; $i ++) {
			$item = $list [$i];
			if ($item ['id'] == $store ['address_id']) {
				array_splice ( $list, $i, 1 );
				array_splice ( $list, 0, 0, array (
						$item 
				) );
			}
		}
		$this->assign ( 'addresses', $list );
		/*
		 * 获取购物车列表
		 */
		$Cart = D ( 'CartView' );
		// //将时间点转换为时间戳
		$dates = array ();
		// $date = strtotime('2015-08-27');
		$date = time ();
		$dates [] = date ( 'Y-m-d', $date );
		$dates [] = date ( 'Y-m-d', $date - 1 * 24 * 60 * 60 );
		$dates [] = date ( 'Y-m-d', $date - 2 * 24 * 60 * 60 );
		$where ['cdate'] = array (
				'in',
				$dates 
		);
		// 输出一天前的日期，在时间戳上减去一天的秒数
		$list = $Cart->where ( $where )->order ( "cdate desc,ctime desc" )->select ();
		$count = count ( $list );
		foreach ( $list as $item ) {
			$dlist [$item ['cdate']] [] = $item;
			$ilist [$item ['id']] = $item;
		}
		$this->assign ( "dlist", $dlist );
		$this->assign ( "ilist", json_encode ( $ilist ) );
		$this->assign ( 'count', $count );
		
		$areas = M ( 'Area' )->order ( array (
				'id' => 'asc' 
		) )->select ();
		$this->assign ( 'areas', $areas );
		/*
		 * 获取下单时间列表
		 */
		$otimes = M ( 'OrderTime' )->order ( array (
				'id' => 'asc' 
		) )->select ();
		$ptimes = array ();
		foreach ( $otimes as $otime ) {
			if ($otime ['status']) {
				$ptime = "";
				switch ($otime ['type']) {
					case 1 :
						$ptime .= "当天";
						break;
					case 2 :
						$ptime .= "次日";
						break;
				}
				$ptime .= "{$otime['stime']}-{$otime['etime']}";
				$ptimes [] = $ptime;
			}
		}
		$this->assign ( 'ptimes', $ptimes );
		$this->display ();
	}
	public function add() {
		$store = session ( 'store' );
		$data ['uid'] = $store ['id'];
		$data ['cid'] = I ( 'cid' );
		$data ['cdate'] = date ( "Y-m-d" );
		$where = $data;
		$cart = D ( 'Cart' );
		$item = $cart->where ( $where )->find ();
		if ($item) {
			$data ['num'] = $item ['num'] + I ( 'num' );
			$result = $cart->where ( $where )->save ( $data );
			if ($result) {
				$json ['result'] = 1;
				$json ['msg'] = "添加到购物车成功";
			} else {
				$json ['result'] = - 1;
				$json ['msg'] = "添加到购物车失败";
			}
		} else {
			$data ['num'] = I ( 'num' );
			$data = $cart->create ( $data );
			$result = $cart->add ( $data );
			if ($result) {
				$json ['result'] = 1;
				$cartnum = $store ['cartnum'] + 1;
				$json ['cartnum'] = $cartnum;
				$store ['cartnum'] = $cartnum;
				session ( 'store', $store );
				$json ['msg'] = "添加到购物车成功";
			} else {
				$json ['result'] = - 2;
				$json ['msg'] = "添加到购物车失败";
			}
		}
		$this->ajaxReturn ( $json );
	}
	public function delete() {
		$where ['id'] = I ( 'id' );
		$cart = M ( 'Cart' );
		$result = $cart->where ( $where )->delete ();
		$this->ajaxReturn ( $result );
	}
	public function balance() {
		$data ['staff'] = "商家";
		$nums = $_POST ['num'];
		$order = D ( "Order" );
		$store = session ( "store" );
		$address = M("Address")->where(array("id"=>$_POST['address_id']))->field("name as username,phone_number as phone,area,address")->find();
		$data = $address;
		$data ['uid'] = $store ['id'];
		$data ['remark'] = $_POST ['remark'];
// 		$data ['address'] = $_POST ['address'];
// 		$data ['address_id'] = $_POST ['address_id'];
// 		$data ['area'] = $_POST ['area'];
		$data ['payment'] = $_POST ['payment'];
		$data ['ptime'] = $_POST ['ptime'];
		$where ['FoodCategory.id'] = array (
				"in",
				$_POST ['category'] 
		);
		$data = $order->create ( $data );
		$result = $order->add ( $data );
		if ($result) {
			$foods = D ("FoodCategory")->where($where)->select();
			// $foods = D ( 'Food' )->where ( $where )->field ( "id as fid,food_name,food_image,yprice,price,unit" )->select ();
			$count = count ( $foods );
			$i = 0;
			$food_yprice = 0;
			$food_price = 0;
			$carrige_price = 0;
			$oddata = array ();
			foreach ( $foods as $food ) {
				$food ['num'] = $nums [$i];
				$food ['oid'] = $data ['id'];
				$oddata [] = $food;
				$food_yprice += $food ['yprice'] * $food ['num'];
				$food_price += $food ['price'] * $food ['num'];
				$i ++;
			}
			M ( 'OrderDetail' )->addAll ( $oddata );
			$data ['food_yprice'] = $food_yprice;
			$data ['food_price'] = $food_price;
			$result = $order->where ( array (
					'id' => $data ['id'] 
			) )->save ( $data );
			if ($result) {
				$where2 ['id'] = array (
						'in',
						$_POST ['cart'] 
				);
				$result = M ( "Cart" )->where ( $where2 )->delete ();
				$store ['cartnum'] = $store ['cartnum'] - count ( $_POST ['category'] );
				$store ['ordernum'] = $store ['ordernum'] + 1;
				session ( 'store', $store );
				$this->success ( "添加订单成功", __APP__ . "/Home/Order" );
			} else {
				$this->error ( "修改订单信息出错" );
			}
		}
	}
}